﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QRCode2.Models;

namespace QRCode2.Controllers
{
    public class UserFavoriteController : Controller
    {
        private UserFavoriteDBContext db = new UserFavoriteDBContext();
        public HomeQRCodeDBContext dbQR = new HomeQRCodeDBContext();
        public HomeForSaleDBContext dbHomeQR = new HomeForSaleDBContext();
        public AgentCommentDBContext dbAgent = new AgentCommentDBContext();

        //
        // GET: /UserFavorite/

        public ActionResult Index()
        {
            return View(db.UserFavorites.ToList());
        }

        //
        // GET: /UserFavorite/Details/5

        public ActionResult Details(int id = 0)
        {
            UserFavorite userfavorite = db.UserFavorites.Find(id);
            if (userfavorite == null)
            {
                return HttpNotFound();
            }
            return View(userfavorite);
        }

        //
        // GET: /UserFavorite/Create

        [Authorize(Roles = "Administrator,HomeBuyer")]
        public ActionResult Create()
        {
            String homeForSaleID = "";
            int homeForSaleInt = 0;

            if (((Request.QueryString["uf"] != null)) && (Request.QueryString["uf"] != ""))
            {
                homeForSaleID = Request.QueryString["uf"];
            }

            if ((homeForSaleID != "") && User.IsInRole("HomeBuyer"))
            {

                homeForSaleInt = Convert.ToInt32(homeForSaleID);

                var homeQR = dbQR.HomeQRCodes.Where(b => b.HomeForSaleID == homeForSaleInt).ToList()[0];
                UserFavorite userFavorite = new UserFavorite();
                userFavorite.HomeQRCode = homeQR.Code;
                return View(userFavorite);
            }

            return View();
        }

        //
        // POST: /UserFavorite/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserFavorite userfavorite)
        {

            String HomeQRCode = Request.Form["HomeQRCode"];

            if (ModelState.IsValid)
            {
                userfavorite.HomeQRCode = HomeQRCode;
                userfavorite.UserName = User.Identity.Name;

                db.UserFavorites.Add(userfavorite);
                db.SaveChanges();

                //Add comment inside AgentComment if ContactAgent = true

                if (userfavorite.ContactAgent)
                {
                    AgentComment ac = new AgentComment();
                    ac.UserName = User.Identity.Name;

                    HomeQRCode hs = dbQR.HomeQRCodes.Where(b => b.Code == HomeQRCode).ToList()[0];
                    HomeForSale hsl = dbHomeQR.HomeForSales.Where(b => b.ID == hs.HomeForSaleID).ToList()[0];

                    ac.AgentUserName = hsl.AgentUserName;
                    ac.Comment = "Added Home to Favorites";
                    ac.CommentTime = DateTime.Now;
                    ac.QRCode = HomeQRCode;
                    ac.Active = true;

                    dbAgent.AgentComments.Add(ac);
                    dbAgent.SaveChanges();

                }


                return RedirectToAction("Index");
            }

            return View(userfavorite);
        }

        //
        // GET: /UserFavorite/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserFavorite userfavorite = db.UserFavorites.Find(id);
            if (userfavorite == null)
            {
                return HttpNotFound();
            }
            return View(userfavorite);
        }

        //
        // POST: /UserFavorite/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserFavorite userfavorite)
        {
            if (ModelState.IsValid)
            {
                db.Entry(userfavorite).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(userfavorite);
        }

        //
        // GET: /UserFavorite/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserFavorite userfavorite = db.UserFavorites.Find(id);
            if (userfavorite == null)
            {
                return HttpNotFound();
            }
            return View(userfavorite);
        }

        //
        // POST: /UserFavorite/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            UserFavorite userfavorite = db.UserFavorites.Find(id);
            db.UserFavorites.Remove(userfavorite);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}